﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlTools.OpenXML.Word
{
    /// <summary>
    /// Word表格边框帮助类
    /// </summary>
    public static class WordTableBordersHelper
    {
        /// <summary>
        /// 为Word表格设置边框
        /// </summary>
        /// <param name="table">要设置边框的表格对象</param>
        /// <param name="settings">边框设置（可选，使用默认设置如果为null）</param>
        public static void SetTableBorders(Table table, TableBorderSettings? settings = null)
        {
            if (settings == null)
            {
                settings = new TableBorderSettings();
            }

            // 获取或创建表格属性
            TableProperties? tableProps = table.GetFirstChild<TableProperties>();
            if (tableProps == null)
            {
                tableProps = new TableProperties();
                table.PrependChild(tableProps);
            }

            // 清除原有边框设置（如果需要）
            if (settings.ClearExisting)
            {
                var existingBorders = tableProps.GetFirstChild<TableBorders>();
                existingBorders?.Remove();
            }

            // 创建新的边框设置
            TableBorders tblBorders = new TableBorders();

            // 设置上边框
            tblBorders.Append(CreateBorderElement(BorderType.Top, settings.OutsideBorder));

            // 设置下边框
            tblBorders.Append(CreateBorderElement(BorderType.Bottom, settings.OutsideBorder));

            // 设置左边框
            tblBorders.Append(CreateBorderElement(BorderType.Left, settings.OutsideBorder));

            // 设置右边框
            tblBorders.Append(CreateBorderElement(BorderType.Right, settings.OutsideBorder));

            // 设置内部水平边框
            tblBorders.Append(CreateBorderElement(BorderType.InsideHorizontal, settings.InsideHorizontalBorder));

            // 设置内部垂直边框
            tblBorders.Append(CreateBorderElement(BorderType.InsideVertical, settings.InsideVerticalBorder));

            // 应用边框设置到表格属性
            tableProps.Append(tblBorders);
        }

        /// <summary>
        /// 边框类型枚举
        /// </summary>
        private enum BorderType
        {
            Top,
            Bottom,
            Left,
            Right,
            InsideHorizontal,
            InsideVertical
        }

        /// <summary>
        /// 创建边框元素
        /// </summary>
        /// <param name="borderType">边框类型</param>
        /// <param name="settings">边框设置</param>
        /// <returns></returns>
        private static OpenXmlElement CreateBorderElement(BorderType borderType, BorderSettings settings)
        {
            switch (borderType)
            {
                case BorderType.Top:
                    return new TopBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                case BorderType.Bottom:
                    return new BottomBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                case BorderType.Left:
                    return new LeftBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                case BorderType.Right:
                    return new RightBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                case BorderType.InsideHorizontal:
                    return new InsideHorizontalBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                case BorderType.InsideVertical:
                    return new InsideVerticalBorder
                    {
                        Val = settings.Style,
                        Size = settings.Size,
                        Color = settings.Color,
                        Space = settings.Space
                    };

                default:
                    return null;
            }
        }
    }

}